<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="150px">
      <el-form-item label="申请人学号" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="申请人学号" @blur="getStuInfo()"></el-input>
<!--                <el-input v-model="dataForm.userId" placeholder="申请人学号"></el-input>-->
      </el-form-item>
      <!--      自动查询-->
      <el-form-item label="申请人姓名">
        <el-input v-model="dataForm.useName" placeholder="申请人姓名" disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="院系">
        <el-input v-model="dataForm.yuanxi" placeholder="院系" disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="班级">
        <el-input v-model="dataForm.banji" placeholder="班级" disabled="true"></el-input>
      </el-form-item>


      <el-form-item label="申请内容" prop="txt">
        <el-input v-model="dataForm.txt" placeholder="申请内容"></el-input>
      </el-form-item>
      <el-form-item label="申请材料" prop="ossUrl">
        <!--      <el-input v-model="dataForm.ossUrl" placeholder="申请材料地址"></el-input>-->
        <SingleUpload v-model="dataForm.ossUrl"></SingleUpload>
      </el-form-item>
      <!--    <el-form-item label="提交时间" prop="tijiaoTime">-->
      <!--      <el-input v-model="dataForm.tijiaoTime" placeholder="提交时间"></el-input>-->
      <!--    </el-form-item>-->
      <!--    <el-form-item label="审批时间" prop="shenpiTime">-->
      <!--      <el-input v-model="dataForm.shenpiTime" placeholder="审批时间"></el-input>-->
      <!--    </el-form-item>-->
      <!--    <el-form-item label="审批人" prop="shenpiUser">-->
      <!--      <el-input v-model="dataForm.shenpiUser" placeholder="审批人"></el-input>-->
      <!--    </el-form-item>-->
      <!--    <el-form-item label="是否通过1：通过 0：未通过/待通过" prop="pass">-->
      <!--      <el-input v-model="dataForm.pass" placeholder="是否通过1：通过 0：未通过/待通过"></el-input>-->
      <!--    </el-form-item>-->
      <el-form-item label="项目类型" prop="xiangmuId">
        <!--        <el-input v-model="dataForm.xiangmuId" placeholder="项目类型id"></el-input>-->
        <el-cascader
          filterable
          placeholder="请选择"
          v-model="xiangmuId"
          :options="categories"
          :props="props"></el-cascader>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="getInfo()">测试</el-button>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from '../../../components/upload/singleUpload.vue'

export default {
  created () {
    this.getXiangmuListTree()
  },
  components: {SingleUpload},
  data () {
    return {
      props: {
        value: 'id',
        label: 'name',
        children: 'childrenCategories'
      },
      visible: false,
      categories: [],
      xiangmuId: [],
      dataForm: {
        id: 0,
        userId: '',
        userName: '',
        status: '',
        yuanxi: '',
        banji: '',
        txt: '',
        ossUrl: '',
        tijiaoTime: '',
        shenpiTime: '',
        shenpiUser: '',
        pass: ''
      },
      dataRule: {
        userId: [
          {required: true, message: '申请人学号不能为空', trigger: 'blur'}
        ],
        txt: [
          {required: true, message: '申请内容不能为空', trigger: 'blur'}
        ],
        ossUrl: [
          {required: true, message: '申请材料地址不能为空', trigger: 'blur'}
        ],
        tijiaoTime: [
          {required: true, message: '提交时间不能为空', trigger: 'blur'}
        ],
        shenpiTime: [
          {required: true, message: '审批时间不能为空', trigger: 'blur'}
        ],
        shenpiUser: [
          {required: true, message: '审批人不能为空', trigger: 'blur'}
        ],
        pass: [
          {required: true, message: '是否通过1：通过 0：未通过/待通过不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    //http://localhost:8080/renren-fast/sys/user/info
    // "user": {
    //   "userId": 7,
    //   "username": "202245225219",
    //   "password": "ae827e832d7314b5cdf0c9b73d4ce24cf04f03d52fd73e6e7ef53c0c408db795",
    //   "salt": "6ewydNSZOZ7A0a23Co62",
    //   "email": "202245225219@jxnhu.edu.cn",
    //   "mobile": "13858948944",
    //   "status": 1,
    //   "roleIdList": null,
    //   "createUserId": 1,
    //   "createTime": "2024-09-16 18:12:41",
    //   "roleNameList": null
    // }
    getInfo () {
      this.$http({
        url: this.$http.adornUrl(`/sys/user/info`),
        menubar: 'get'
      }).then(({data}) => { //解构 获得下一层的data
        // console.log(data)
        this.dataForm.userId = data.user.username
      })
    },

    getStuInfo () {
      //查询dataForm.userId学号对应的信息info
      console.log(this.dataForm.userId)
      this.$http({
        url: this.$http.adornUrl(`/stu/users/info/${this.dataForm.userId}`),
        menubar: 'get'
      }).then(({data}) => { //解构 获得下一层的data
        console.log(data)
        this.dataForm.useName = data.users.username
        this.dataForm.yuanxi = data.users.yuanxi
        this.dataForm.banji = data.users.banji
      })
    },

    getXiangmuListTree () {
      this.$http({
        // url: 'http://localhost:9090/commodity/category/list/tree',
        url: this.$http.adornUrl('/stu/xiangmutypelevel/list/singleTree'),
        menubar: 'get'
      }).then(({data}) => { //解构 获得下一层的data
        console.log(data.listTree)
        this.categories = data.listTree
      })
    },

    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/stu/shenqinsingle/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.userId = data.shenqinSingle.userId
              this.dataForm.txt = data.shenqinSingle.txt
              this.dataForm.ossUrl = data.shenqinSingle.ossUrl
              this.dataForm.tijiaoTime = data.shenqinSingle.tijiaoTime
              this.dataForm.shenpiTime = data.shenqinSingle.shenpiTime
              this.dataForm.shenpiUser = data.shenqinSingle.shenpiUser
              this.dataForm.pass = data.shenqinSingle.pass
              this.dataForm.xiangmuId = data.shenqinSingle.xiangmuId
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
          console.log(this.dataForm)
          console.log(this.xiangmuId)
          if (valid) {

            this.$http({
              url: this.$http.adornUrl(`/stu/shenqinsingle/save`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId,
                'txt': this.dataForm.txt,
                'ossUrl': this.dataForm.ossUrl,
                'xiangmuId': this.xiangmuId[this.xiangmuId.length - 1],
                'tijiaoTime': this.dataForm.tijiaoTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })

          }
        }
      )
    }

  }
}
</script>
